<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Data_fetcher extends CI_Model {
	
	
	function fetch_table($table,$order=null,$limit=null,$where=null)
	{				
		
		if($order!=null)
			$this->db->order_by($order[0],$order[1]);
		elseif($table == 'tb_mas_content')
			$this->db->order_by('dteStart','desc');
		
		if($limit!=null)
			$this->db->limit($limit);
			
		if($where!=null)
			$this->db->where($where);
		
		$data =  $this->db
						->get($table)
						->result_array();
						
		return $data;
						
	}
    
    function fetch_table_by_community($table,$community,$order=null,$limit=null,$where=null)
	{				
		
		if($order!=null)
			$this->db->order_by($order[0],$order[1]);
		elseif($table == 'tb_mas_content')
			$this->db->order_by('dteStart','desc');
		
		if($limit!=null)
			$this->db->limit($limit);
			
		if($where!=null)
			$this->db->where($where);
        
        
        $this->db->where('fk-tb_mas_community',$community);
		
		$data =  $this->db
						->get($table)
						->result_array();
						
		return $data;
						
	}
	
    
    function fetch_ads_by_section($section=null)
    {   
        //print_r($section);
        
        if($section!=null){
            $data = $this->db
                         ->where(Array('fk-tb_sys_adspace'=>$section))
                         ->get('tb_sys_advertising')
                         ->result_array();
                               
            
            return $data;
        }
        else
        {
            return null;
        }
       
    }
    function fetch_single_ad($id=1)
	{
		$data = $this->db
					 ->get_where('tb_sys_advertising',array('intID'=>$id))
					 ->result_array();
		return current($data);
	}
    function remove_ad($id=1)
    {
        $this->db->delete('tb_sys_advertising',array('intID'=>$id));
    }
    
    function new_ad($post)
    {
        $this->db->insert('tb_sys_advertising',$post);
        return $this->db->insert_id();
    }
    
    function update_ad($post)
    {
        $this->db->where('intID',$post['intID'])->update('tb_sys_advertising',$post);
    }
	function fetch_single_data($table,$id=1)
	{
		$data = $this->db
					 ->get_where($table,array('intId'=>$id))
					 ->result_array();
		return current($data);
	}
	
	
	function count_results($table)
	{
		return $this->db->count_all($table);
	}
	
	function get_validation_items($table)
	{
		$data = $this->db
					 ->get_where('su-tb_sys_validation',array('strTable'=>$table))
					 ->result_array();
		return $data;
	}
	
	
	
	

}